package com.warrantyhotel.android.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.adapter.NewsAdapter;
import com.warrantyhotel.android.db.QueriesCodes;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.News_RSS;
import com.warrantyhotel.android.model.Keys;
import com.warrantyhotel.android.service.UpdateIntentService;
import com.warrantyhotel.android.util.DetachableResultReceiver;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler;
import com.warrantyhotel.android.util.DetachableResultReceiver.Receiver;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler.AsyncQueryListener;

public class NewsActivity extends Activity implements Receiver, AsyncQueryListener, OnItemClickListener {

	private DetachableResultReceiver receiver;
	private NotifyingAsyncQueryHandler mHandler;
	private ListView newsList;
	private NewsAdapter mAdapter;
	private ProgressDialog progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.news);

		newsList = (ListView) findViewById(R.id.news_list);
		newsList.setOnItemClickListener(this);

		mHandler = new NotifyingAsyncQueryHandler(getContentResolver(), this);
		retrieveNewsRSS();

		receiver = new DetachableResultReceiver(new Handler());
		receiver.setReceiver(this);

		Intent userInfoIntent = new Intent(Intent.ACTION_SYNC, News_RSS.CONTENT_URI, this, UpdateIntentService.class);
		userInfoIntent.putExtra(UpdateIntentService.EXTRA_STATUS_RECEIVER, receiver);
		userInfoIntent.putExtra(UpdateIntentService.EXTRA_REQUEST_CODE, QueriesCodes.NEWS_RSS);
		startService(userInfoIntent);
	}

	private void retrieveNewsRSS() {
		String[] projectionNews = { News_RSS._ID, News_RSS.TITLE, News_RSS.LINK, News_RSS.LINK_BANNER, News_RSS.LINK_THUMBNAIL,
				News_RSS.GUID, News_RSS.DESCRIPTION, News_RSS.DATE_UPDATE };
		mHandler.startQuery(QueriesCodes.NEWS_RSS, News_RSS.CONTENT_URI, projectionNews, null, null, null);
	}

	private void retrieveNewsRSSbyId(Long id) {
		String[] projectionNews = { News_RSS._ID, News_RSS.TITLE, News_RSS.LINK_BANNER, News_RSS.LINK_THUMBNAIL, News_RSS.LINK,
				News_RSS.GUID, News_RSS.DESCRIPTION, News_RSS.DATE_UPDATE };
		mHandler.startQuery(QueriesCodes.NEWS_RSS_by_GUID, News_RSS.CONTENT_URI, projectionNews, News_RSS._ID + " = " + id, null,
				null);
	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		switch (resultCode) {
		case UpdateIntentService.STATUS_RUNNING:
			// do nothing
			break;
		case UpdateIntentService.STATUS_ERROR:
			if (progress != null)
				progress.dismiss();
			break;
		case UpdateIntentService.STATUS_FINISHED:
			if (progress != null)
				progress.dismiss();
			retrieveNewsRSS();
			break;
		}
	}

	@Override
	public void onQueryComplete(int token, Object cookie, Cursor cursor) {
		switch (token) {
		case QueriesCodes.NEWS_RSS:
			if (cursor != null) {
				if (cursor.getCount() == 0)

				startManagingCursor(cursor);
				if (mAdapter == null) {
					mAdapter = new NewsAdapter(getApplicationContext(), cursor);
					newsList.setAdapter(mAdapter);
				} else {
					mAdapter.changeCursor(cursor);
					mAdapter.notifyDataSetChanged();
				}
			}
			break;
		case QueriesCodes.NEWS_RSS_by_GUID:
			if (cursor != null) {
				startManagingCursor(cursor);
				if (cursor.moveToFirst()) {
					String guid = cursor.getString(cursor.getColumnIndex(News_RSS.GUID));
					Intent intent = new Intent(getApplicationContext(), NewsActivityDetail.class);
					intent.putExtra(Keys._guid, guid);
					startActivity(intent);
				}
			}
			break;
		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {

		retrieveNewsRSSbyId(id);

	}
}
